package org.point.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.point.common.controller.BaseController;
import org.point.common.util.DoPage;
import org.point.domain.CreditsRecord;
import org.point.domain.CreditsRule;
import org.point.service.CreditsRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;

/**
 * 
 * @ClassName: CreditsRecordController  
 * @Description: TODO(积分规则记录)  
 * @author Kenny_Z' szqiuk@gmail.com
 * @date 2016年10月23日 下午9:18:11  
 *
 */
@Controller
@RequestMapping("/creditsRec/")
public class CreditsRecordController extends BaseController {

	@Autowired
	private CreditsRecordService service;
	
	/**
	 * 
	 * @Title: credutsRecordList 
	 * @Description: TODO(查询积分记录列表) 
	 * @param @param memberId
	 * @param @param currentPage
	 * @param @param response    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	@RequestMapping(value = "crlist.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void credutsRecordList(@RequestParam(value = "memberId") String memberId, @RequestParam(value = "currentPaeg") Integer currentPage, HttpServletResponse response){
		
		JSONObject jsonObj = new JSONObject();
		DoPage<CreditsRecord> page = new DoPage<CreditsRecord>();
		Map<String, Object> map = new HashMap<String, Object>();
		
		try {
			if(strOperateUtil.isNullForString(memberId)){
				map.put("memberId", memberId);
				if(currentPage != null){
					page.setCurrentPage(currentPage);
					map.put("firstRecord", page.getFirstRecord());
					map.put("pageSize", page.getPageSize());
					DoPage<CreditsRecord> resultPage = service.searchCRForPage(page);
					// 积分记录分页查询成功返回数据及状态
					jsonObj.put("page", resultPage);
					jsonObj.put("resultMsg", 1);
				} else {
					jsonObj.put("resultMsg", -2);
				}
			} else {
				// 会员单位ID为null
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", 0);
			throw new RuntimeException("积分规则记录查询错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}
	}

	/**
	 * 
	 * @Title: insertCRec 
	 * @Description: TODO(新增积分记录) 
	 * @param @param record    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	@RequestMapping(value = "crecinsert.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void insertCRec(@ModelAttribute("CreditsRecord") CreditsRecord record, HttpServletResponse response){
		JSONObject jsonObj = new JSONObject();
		Integer result = 0;
		
		try {
			if(record != null){
				// 新增积分记录
				Integer resutl = service.insertCreditsRecord(record);
				jsonObj.put("resultMsg", resutl);
			} else {
				// 积分记录参数为null
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", 0);
			throw new RuntimeException("新增积分规则错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}
	}
	
}
